package com.legsem.jrhosync.ws.rs;

import com.legsem.jhosync.Constants;
import com.legsem.jhosync.dto.ClientLogin;

public class ApiResourceITCase extends AbstractResourceITCase {

    public void testAdminUserShouldGetApiToken() throws Exception {
        ClientLogin clientLogin = new ClientLogin();
        clientLogin.setLogin(Constants.ADMIN_LOGIN);
        clientLogin.setPassword("");
        postJSONRequest("/login", clientLogin);
        byte[] response = postJSONRequest("/api/get_api_token", "");
        String token = new String(response);
        assertEquals(32, token.length());

    }

    public void testNonAdminUserShouldNotGetApiToken() throws Exception {
        ClientLogin clientLogin = new ClientLogin();
        clientLogin.setLogin("pipo");
        clientLogin.setPassword("");
        postJSONRequest("/login", clientLogin);
        try {
            postJSONRequest("/api/get_api_token", "");
            fail();
        } catch (Exception e) {
            assertTrue(e.getMessage().contains("Request returned:422"));
            assertTrue(e.getMessage().contains("Invalid/missing API user"));
        }
    }

    public void testAdminUserShouldReset() throws Exception {
        ClientLogin clientLogin = new ClientLogin();
        clientLogin.setLogin(Constants.ADMIN_LOGIN);
        clientLogin.setPassword("");
        postJSONRequest("/login", clientLogin);
        byte[] response = postJSONRequest("/api/get_api_token", "");
        String token = new String(response);
        response = postJSONRequest("/api/reset", "{\"api_token\":\"" + token
                + "\"}");
        assertEquals("DB reset", new String(response));

    }
}
